// pages/device/device.js
const app = getApp();
const {
  deviceList,
  unbindBox,
  clear
} = require('../../config.js');
let timer ;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    index: 1,
    dataList0: [],
    pageBean0: {},
    dataList1: [],
    pageBean1: {},
    dataList2: [],
    pageBean2: {},
    dataList3: [],
    pageBean3: {},
    nameLike: '',
    maintType: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let { type } = options;
    // console.log(type)
    this.setData({
      index: type,
      maintType: app.globalData.maintType
    })
    // this.maintType = app.globalData.maintType
    // console.log('maintType+', this.maintType)
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    wx.showNavigationBarLoading();
    this.setData({
      dataList0: [],
      dataList1: [],
      dataList2: [],
      dataList3: [],
    })
    app.appRequest({
      url: deviceList,
      success: (res) => {
        wx.hideNavigationBarLoading();
        let compact = res.data.compact;
        let pageBean = res.data.pageBean;
        let dataList = res.data.pageBean.result.map((item) => {
          if (compact[item.id]) {
            item.blackInit = compact[item.id].blackInit;
            item.coloursInit = compact[item.id].coloursInit
          }
          return item;
        }) 
        this.setData({
          dataList0:dataList,
          pageBean0:pageBean
        })
      }
    }),
    app.appRequest({
      url: deviceList,
      data: {
        isSigned: 0
      },
      success: (res) => {
        let compact = res.data.compact;
        let pageBean = res.data.pageBean;
        let dataList = res.data.pageBean.result.map((item) => {
          if (compact[item.id]) {
            item.blackInit = compact[item.id].blackInit;
            item.coloursInit = compact[item.id].coloursInit
          }
          return item;
        })
        this.setData({
          dataList1: dataList,
          pageBean1: pageBean
        })
      }
    }),
    app.appRequest({
      url: deviceList,
      data: {
        isSigned: 1
      },
      success: (res) => {
        let compact = res.data.compact;
        let pageBean = res.data.pageBean;
        let dataList = res.data.pageBean.result.map((item) => {
          if (compact[item.id]) {
            item.blackInit = compact[item.id].blackInit;
            item.coloursInit = compact[item.id].coloursInit
          }
          return item;
        })
        this.setData({
          dataList2: dataList,
          pageBean2: pageBean
        })
      }
    }),
    app.appRequest({
      url: deviceList,
      data: {
        isSigned: 2
      },
      success: (res) => {
        let compact = res.data.compact;
        let pageBean = res.data.pageBean;
        let dataList = res.data.pageBean.result.map((item) => {
          if (compact[item.id]) {
            item.blackInit = compact[item.id].blackInit;
            item.coloursInit = compact[item.id].coloursInit
          }
          return item;
        })
        this.setData({
          dataList3: dataList,
          pageBean3: pageBean
        })
      }
    })
  },


  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.showNavigationBarLoading();
    app.appRequest({
      url: deviceList,
      data: {
        nameLike: this.data.nameLike,
      },
      success: (res) => {
        wx.hideNavigationBarLoading();
        wx.stopPullDownRefresh();
        let compact = res.data.compact;
        let pageBean = res.data.pageBean;
        let dataList = res.data.pageBean.result.map((item) => {
          if (compact[item.id]) {
            item.blackInit = compact[item.id].blackInit;
            item.coloursInit = compact[item.id].coloursInit
          }
          return item;
        })
        this.setData({
          dataList0: dataList,
          pageBean0: pageBean
        })
      }
    }),
    app.appRequest({
      url: deviceList,
      data: {
        nameLike: this.data.nameLike,
        isSigned: 0
      },
      success: (res) => {
        wx.hideNavigationBarLoading();
        let compact = res.data.compact;
        let pageBean = res.data.pageBean;
        let dataList = res.data.pageBean.result.map((item) => {
          if (compact[item.id]) {
            item.blackInit = compact[item.id].blackInit;
            item.coloursInit = compact[item.id].coloursInit
          }
          return item;
        })
        this.setData({
          dataList1: dataList,
          pageBean1: pageBean
        })
      }
    }),
    app.appRequest({
      url: deviceList,
      data: {
        nameLike: this.data.nameLike,
        isSigned: 1
      },
      success: (res) => {
        wx.hideNavigationBarLoading();
        let compact = res.data.compact;
        let pageBean = res.data.pageBean;
        let dataList = res.data.pageBean.result.map((item) => {
          if (compact[item.id]) {
            item.blackInit = compact[item.id].blackInit;
            item.coloursInit = compact[item.id].coloursInit
          }
          return item;
        })
        this.setData({
          dataList2: dataList,
          pageBean2: pageBean
        })
      }
    }),
    app.appRequest({
      url: deviceList,
      data: {
        nameLike: this.data.nameLike,
        isSigned: 2
      },
      success: (res) => {
        wx.hideNavigationBarLoading();
        let compact = res.data.compact;
        let pageBean = res.data.pageBean;
        let dataList = res.data.pageBean.result.map((item) => {
          if (compact[item.id]) {
            item.blackInit = compact[item.id].blackInit;
            item.coloursInit = compact[item.id].coloursInit
          }
          return item;
        })
        this.setData({
          dataList3: dataList,
          pageBean3: pageBean
        })
      }
    })
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let pageBean0 = this.data.pageBean0;
    let pageBean1 = this.data.pageBean1;
    let pageBean2 = this.data.pageBean2;
    let pageBean3 = this.data.pageBean3;
    // 所有设备
    if (this.data.index == 1) {
      if (pageBean0.hasNextPage) {
        app.appRequest({
          url: deviceList,
          data: {
            nameLike: this.data.nameLike,
            page: pageBean0.curPage + 1,
            pageSize: pageBean0.pageSize,
          },
          success: (res) => {
            wx.hideNavigationBarLoading();
            let compact = res.data.compact;
            let pageBean = res.data.pageBean;
            let newDataList = res.data.pageBean.result.map((item) => {
              if (compact[item.id]) {
                item.blackInit = compact[item.id].blackInit;
                item.coloursInit = compact[item.id].coloursInit
              }
              return item;
            })
            let dataList = [...this.data.dataList0, ...newDataList]
            
            this.setData({
              dataList0: dataList,
              pageBean0: pageBean
            })
          }
        })
      }
    }
    // 待签约设备
    if (this.data.index == 2) {
      if (pageBean1.hasNextPage) {
        app.appRequest({
          url: deviceList,
          data: {
            page: pageBean1.curPage + 1,
            pageSize: pageBean1.pageSize,
            nameLike: this.data.nameLike,
            isSigned: 0
          },
          success: (res) => {
            wx.hideNavigationBarLoading();
            let compact = res.data.compact;
            let pageBean = res.data.pageBean;
            let newDataList = res.data.pageBean.result.map((item) => {
              if (compact[item.id]) {
                item.blackInit = compact[item.id].blackInit;
                item.coloursInit = compact[item.id].coloursInit
              }
              return item;
            })
            let dataList = [...this.data.dataList1, ...newDataList]
            // console.log(dataList)
            this.setData({
              dataList1: dataList,
              pageBean1: pageBean
            })
          }
        })
      }
    }
    // 签约中设备
    if (this.data.index == 3) {
      if (pageBean2.hasNextPage) {
        app.appRequest({
          url: deviceList,
          data: {
            page: pageBean2.curPage + 1,
            pageSize: pageBean2.pageSize,
            nameLike: this.data.nameLike,
            isSigned: 1
          },
          success: (res) => {
            wx.hideNavigationBarLoading();
            let compact = res.data.compact;
            let pageBean = res.data.pageBean;
            let newDataList = res.data.pageBean.result.map((item) => {
              if (compact[item.id]) {
                item.blackInit = compact[item.id].blackInit;
                item.coloursInit = compact[item.id].coloursInit
              }
              return item;
            })
            let dataList = [...this.data.dataList2, ...newDataList]
            this.setData({
              dataList2: dataList,
              pageBean2: pageBean
            })
          }
        })
      }
    }
    // 待清算设备
    if (this.data.index == 4) {
      if (pageBean3.hasNextPage) {
        app.appRequest({
          url: deviceList,
          data: {
            page: pageBean3.curPage + 1,
            pageSize: pageBean3.pageSize,
            nameLike: this.data.nameLike,
            isSigned: 2
          },
          success: (res) => {
            wx.hideNavigationBarLoading();
            let compact = res.data.compact;
            let pageBean = res.data.pageBean;
            let newDataList = res.data.pageBean.result.map((item) => {
              if (compact[item.id]) {
                item.blackInit = compact[item.id].blackInit;
                item.coloursInit = compact[item.id].coloursInit
              }
              return item;
            })
            let dataList = [...this.data.dataList3, ...newDataList]
            this.setData({
              dataList3: dataList,
              pageBean3: pageBean
            })
          }
        })
      }
    }
  },

  changeIndex: function (e) {
    console.log(e.target.dataset.index)
    let index = e.target.dataset.index;
    this.setData({
      index: index
    })
  },
  bindDevice: function(e){
    let { id, type, status, issigned, location, codes, blackinit, coloursinit } = e.currentTarget.dataset;
    // console.log(e.currentTarget.dataset)
    if(codes == ' ') codes = ''
    wx.navigateTo({
      url: `/pages/bind_device/bind_device?id=${id}&type=${type}&status=${status}&isSigned=${issigned}&blackInit=${(blackinit || blackinit === 0) ? blackinit : ''}&coloursInit=${(coloursinit || coloursinit === 0) ? coloursinit : ''}&codes=${codes || ''}&location=${location || ''}`,
    });
  },
  clear: function(e){
    let id = e.currentTarget.dataset.id;
    let that = this;
    wx.showModal({
      title: '清算设备',
      content: '请您确认是否要清算设备',
      success(res) {
        if (res.confirm) {
          app.appRequest({
            url: clear,
            method: 'POST',
            data:{
              id
            },
            success: (res) => {
              if(res.code==0){
                app.appRequest({
                  url: deviceList,
                  success: (res) => {
                    wx.hideNavigationBarLoading();
                    wx.stopPullDownRefresh();
                    let compact = res.data.compact;
                    let pageBean = res.data.pageBean;
                    let dataList = res.data.pageBean.result.map((item) => {
                      if (compact[item.id]) {
                        item.blackInit = compact[item.id].blackInit;
                        item.coloursInit = compact[item.id].coloursInit
                      }
                      return item;
                    })
                    that.setData({
                      dataList0: dataList,
                      pageBean0: pageBean
                    })
                  }
                }),
                app.appRequest({
                  url: deviceList,
                  data: {
                    isSigned: 0
                  },
                  success: (res) => {
                    wx.hideNavigationBarLoading();
                    let compact = res.data.compact;
                    let pageBean = res.data.pageBean;
                    let dataList = res.data.pageBean.result.map((item) => {
                      if (compact[item.id]) {
                        item.blackInit = compact[item.id].blackInit;
                        item.coloursInit = compact[item.id].coloursInit
                      }
                      return item;
                    })
                    that.setData({
                      dataList1: dataList,
                      pageBean1: pageBean
                    })
                  }
                }),
                app.appRequest({
                  url: deviceList,
                  data: {
                    isSigned: 1
                  },
                  success: (res) => {
                    wx.hideNavigationBarLoading();
                    let compact = res.data.compact;
                    let pageBean = res.data.pageBean;
                    let dataList = res.data.pageBean.result.map((item) => {
                      if (compact[item.id]) {
                        item.blackInit = compact[item.id].blackInit;
                        item.coloursInit = compact[item.id].coloursInit
                      }
                      return item;
                    })
                    that.setData({
                      dataList2: dataList,
                      pageBean2: pageBean
                    })
                  }
                }),
                app.appRequest({
                  url: deviceList,
                  data: {
                    isSigned: 2
                  },
                  success: (res) => {
                    wx.hideNavigationBarLoading();
                    let compact = res.data.compact;
                    let pageBean = res.data.pageBean;
                    let dataList = res.data.pageBean.result.map((item) => {
                      if (compact[item.id]) {
                        item.blackInit = compact[item.id].blackInit;
                        item.coloursInit = compact[item.id].coloursInit
                      }
                      return item;
                    })
                    that.setData({
                      dataList3: dataList,
                      pageBean3: pageBean
                    })
                  }
                })
              }
            }
          })
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },
  deviceChange: function (e) {
    let nameLike = e.detail.value;
    this.setData({
      nameLike: nameLike
    })
    clearTimeout(timer);
    timer = setTimeout(() => {
      wx.showLoading({
        title: '加载中...',
      })
          
      app.appRequest({
        url: deviceList,
        data: {
          nameLike
        },
        success: (res) => {
          wx.hideNavigationBarLoading();
          let compact = res.data.compact;
          let pageBean = res.data.pageBean;
          let dataList = res.data.pageBean.result.map((item) => {
            if (compact[item.id]) {
              item.blackInit = compact[item.id].blackInit;
              item.coloursInit = compact[item.id].coloursInit
            }
            return item;
          })
          this.setData({
            dataList0: dataList,
            pageBean0: pageBean
          })
          wx.hideLoading()
        },
        fail: () => {
          wx.hideLoading()
        }
      })
      app.appRequest({
        url: deviceList,
        data: {
          nameLike,
          isSigned: 0
        },
        success: (res) => {
          wx.hideNavigationBarLoading();
          let compact = res.data.compact;
          let pageBean = res.data.pageBean;
          let dataList = res.data.pageBean.result.map((item) => {
            if (compact[item.id]) {
              item.blackInit = compact[item.id].blackInit;
              item.coloursInit = compact[item.id].coloursInit
            }
            return item;
          })
          this.setData({
            dataList1: dataList,
            pageBean1: pageBean
          })
        },
        fail: () => {
          wx.hideLoading()
        }
      })
      app.appRequest({
        url: deviceList,
        data: {
          nameLike,
          isSigned: 1
        },
        success: (res) => {
          wx.hideNavigationBarLoading();
          let compact = res.data.compact;
          let pageBean = res.data.pageBean;
          let dataList = res.data.pageBean.result.map((item) => {
            if (compact[item.id]) {
              item.blackInit = compact[item.id].blackInit;
              item.coloursInit = compact[item.id].coloursInit
            }
            return item;
          })
          this.setData({
            dataList2: dataList,
            pageBean2: pageBean
          })
        },
        fail: () => {
          wx.hideLoading()
        }
      })
      app.appRequest({
        url: deviceList,
        data: {
          nameLike,
          isSigned: 2
        },
        success: (res) => {
          wx.hideNavigationBarLoading();
          let compact = res.data.compact;
          let pageBean = res.data.pageBean;
          let dataList = res.data.pageBean.result.map((item) => {
            if (compact[item.id]) {
              item.blackInit = compact[item.id].blackInit;
              item.coloursInit = compact[item.id].coloursInit
            }
            return item;
          })
          this.setData({
            dataList3: dataList,
            pageBean3: pageBean
          })
        },
        fail: () => {
          wx.hideLoading()
        }
      })
    }, 1000)
  },
  clearInput: function(){
    this.setData({
      nameLike:''
    })
    app.appRequest({
      url: deviceList,
      success: (res) => {
        let compact = res.data.compact;
        let pageBean = res.data.pageBean;
        let dataList = res.data.pageBean.result.map((item) => {
          if (compact[item.id]) {
            item.blackInit = compact[item.id].blackInit;
            item.coloursInit = compact[item.id].coloursInit
          }
          return item;
        })
        this.setData({
          dataList0: dataList,
          pageBean0: pageBean
        })
      }
    }),
      app.appRequest({
        url: deviceList,
        data: {
          isSigned: 0
        },
        success: (res) => {
          let compact = res.data.compact;
          let pageBean = res.data.pageBean;
          let dataList = res.data.pageBean.result.map((item) => {
            if (compact[item.id]) {
              item.blackInit = compact[item.id].blackInit;
              item.coloursInit = compact[item.id].coloursInit
            }
            return item;
          })
          this.setData({
            dataList1: dataList,
            pageBean1: pageBean
          })
        }
      }),
      app.appRequest({
        url: deviceList,
        data: {
          isSigned: 1
        },
        success: (res) => {
          let compact = res.data.compact;
          let pageBean = res.data.pageBean;
          let dataList = res.data.pageBean.result.map((item) => {
            if (compact[item.id]) {
              item.blackInit = compact[item.id].blackInit;
              item.coloursInit = compact[item.id].coloursInit
            }
            return item;
          })
          this.setData({
            dataList2: dataList,
            pageBean2: pageBean
          })
        }
      }),
      app.appRequest({
        url: deviceList,
        data: {
          isSigned: 2
        },
        success: (res) => {
          let compact = res.data.compact;
          let pageBean = res.data.pageBean;
          let dataList = res.data.pageBean.result.map((item) => {
            if (compact[item.id]) {
              item.blackInit = compact[item.id].blackInit;
              item.coloursInit = compact[item.id].coloursInit
            }
            return item;
          })
          this.setData({
            dataList3: dataList,
            pageBean3: pageBean
          })
        }
      })
  },
  // 解绑盒子
  unbindDevice: function (e) {
    let that = this
    let nameLike = this.data.nameLike
    wx.showModal({
      title: '解绑盒子',
      content: '请确认是否要解绑该盒子',
      success(res) {
        if (res.confirm) {
          app.appRequest({
            url: unbindBox,
            data: {
              id: e.target.dataset.id
            },
            method: 'POST',
            success: (res) => {
              console.log(res)
              if(res.code === 0){
                wx.showToast({
                  title: '解绑成功',
                  icon: 'success'
                })
                // 刷新列表
                app.appRequest({
                  url: deviceList,
                  data: {
                    nameLike
                  },
                  success: (res) => {
                    wx.hideNavigationBarLoading();
                    let compact = res.data.compact;
                    let pageBean = res.data.pageBean;
                    let dataList = res.data.pageBean.result.map((item) => {
                      if (compact[item.id]) {
                        item.blackInit = compact[item.id].blackInit;
                        item.coloursInit = compact[item.id].coloursInit
                      }
                      return item;
                    })
                    that.setData({
                      dataList0: dataList,
                      pageBean0: pageBean
                    })
                    wx.hideLoading()
                  }
                })
                app.appRequest({
                  url: deviceList,
                  data: {
                    nameLike,
                    isSigned: 0
                  },
                  success: (res) => {
                    wx.hideNavigationBarLoading();
                    let compact = res.data.compact;
                    let pageBean = res.data.pageBean;
                    let dataList = res.data.pageBean.result.map((item) => {
                      if (compact[item.id]) {
                        item.blackInit = compact[item.id].blackInit;
                        item.coloursInit = compact[item.id].coloursInit
                      }
                      return item;
                    })
                    that.setData({
                      dataList1: dataList,
                      pageBean1: pageBean
                    })
                  }
                })
              }else{
                wx.showToast({
                  title: res.msg,
                  icon: 'none'
                })
              }
            }
          })
        }
      }
    })
  }
})